//
//  NSString+Add.m
//  Merchant
//
//  Created by ibendi on 16/3/21.
//  Copyright © 2016年 ibendi. All rights reserved.
//

#import "NSString+Hellp.h"
#import <UIKit/UIKit.h>

// NSData (Add)  需要引入
#import <CommonCrypto/CommonCrypto.h>


@implementation NSData (Hellp)

- (NSString *)md5String
{
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(self.bytes, (CC_LONG)self.length, result);
    return [NSString stringWithFormat:
            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];
}

@end

@implementation NSString (Add)
- (NSString *)md5String {
    return [[self dataUsingEncoding:NSUTF8StringEncoding] md5String];
}

- (CGSize)sizeForFont:(UIFont *)font size:(CGSize)size mode:(NSLineBreakMode)lineBreakMode {
    CGSize result;
    if (!font) font = [UIFont systemFontOfSize:12];
    if ([self respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) {
        NSMutableDictionary *attr = [NSMutableDictionary new];
        attr[NSFontAttributeName] = font;
        if (lineBreakMode != NSLineBreakByWordWrapping) {
            NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
            paragraphStyle.lineBreakMode = lineBreakMode;
            attr[NSParagraphStyleAttributeName] = paragraphStyle;
        }
        CGRect rect = [self boundingRectWithSize:size
                                         options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                      attributes:attr context:nil];
        result = rect.size;
    } else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
        result = [self sizeWithFont:font constrainedToSize:size lineBreakMode:lineBreakMode];
#pragma clang diagnostic pop
    }
    return result;
}

- (CGFloat)widthForFont:(UIFont *)font {
    CGSize size = [self sizeForFont:font size:CGSizeMake(HUGE, HUGE) mode:NSLineBreakByWordWrapping];
    return size.width;
}

- (CGFloat)heightForFont:(UIFont *)font width:(CGFloat)width {
    CGSize size = [self sizeForFont:font size:CGSizeMake(width, HUGE) mode:NSLineBreakByWordWrapping];
    return size.height;
}

- (BOOL)matchesRegex:(NSString *)regex options:(NSRegularExpressionOptions)options {
    NSRegularExpression *pattern = [NSRegularExpression regularExpressionWithPattern:regex options:options error:NULL];
    if (!pattern) return NO;
    return ([pattern numberOfMatchesInString:self options:0 range:NSMakeRange(0, self.length)] > 0);
}

- (BOOL)validatePhone {
    return [self matchesRegex:@"1[3|4|5|7|8|][0-9]{9}" options:NSRegularExpressionDotMatchesLineSeparators];
}

- (BOOL)validateEmail {
    return [self matchesRegex:@"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}" options:NSRegularExpressionCaseInsensitive];
}

- (BOOL)validateURL {
    return [self matchesRegex:@"(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|]" options:NSRegularExpressionCaseInsensitive];
}



- (NSString *)timeWithFormat:(NSString *)format {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    if (format) {
        [formatter setDateFormat:format];
    }else {
        [formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
    }
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[self integerValue]];
    
    return [formatter stringFromDate:date];
}

+ (NSString *)showStarWithNum:(NSInteger)num
{
    NSString *star  = [NSString stringWithFormat:@"★★★★★"];
    
    switch (num) {
        case 1:
            star = @"★";
            break;
        case 2:
            star = @"★★";
            break;
        case 3:
            star = @"★★★";
            break;
        case 4:
            star = @"★★★★";
            break;
        case 5:
            star = @"★★★★★";
            break;
        default:
            star = @"★★★★★";
            break;
    }
    return star;
    
}

+(BOOL)validKey:(NSString*)key
{
    if( key==nil || key.length !=16 ){
        return NO;
    }
    return YES;
}



@end
